package net.bolue.console.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.data.UpdateInfo;
import net.bolue.console.data.model.UpdateInfoModel;
import net.bolue.sysmng.db.DbCreateSqlUtil;
import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ColInfoModel;
import net.bolue.sysmng.tblbean.ColInfo;
import net.bolue.sysmng.tblbean.UpdRes;
import net.bolue.sysmng.util.ReflectHelper;
import net.bolue.web.mvc.ActionResult;
import net.bolue.web.util.RequestUtil;

/**
 * 更新操作业务
 * @author Administrator
 *
 */
public class UpdateModel extends BusiBase{

	private String updSign;//单元标识
	private UpdateInfo updateInfo;
	private UpdateInfoModel updateInfoModel;
	private ColInfoModel colInfoModel;
	
	public UpdateModel(){}
	
	public UpdateModel(HttpServletRequest request,UpdateInfo updateInfo)throws BolueException{
		super(request);
		this.updateInfo = updateInfo;
	}
	
	public UpdateModel(HttpServletRequest request,UpdateInfo updateInfo,String updSign)throws BolueException{
		super(request);
		this.updateInfo = updateInfo;
		this.updSign = updSign;
	}
	
	public UpdateModel(HttpServletRequest request,UpdRes updRes)throws BolueException{
		super(request);
		updateInfoModel = new UpdateInfoModel(request,updRes);
		this.updateInfo = updateInfoModel.getUpdInfo();
	}
	
	/**
	 * 初始化表和表列
	 * @throws BolueException
	 */
	private void init()throws BolueException{
		if(this.updateInfo==null){
			return;
		}
		UpdRes updRes = this.updateInfo.getUpdRes();
		if(updRes==null){
			return;
		}
		String tableName = updRes.getTableName();
		List<ColInfo> colInfoList = this.updateInfo.getColInfoList();
		if(colInfoList==null){
			colInfoModel = new ColInfoModel();
			colInfoList = colInfoModel.getColInfoList(updRes.getSubSys(), tableName);
		}
		List<ColInfo> keyColInfoList = this.updateInfo.getKeyColInfoList();
		if(keyColInfoList==null){
			keyColInfoList = colInfoModel.getKeyColInfoList(updRes.getSubSys(), tableName);
		}
		Map<String, ColInfo> colInfoMap = this.updateInfo.getColInfoMap();
		if(colInfoMap==null){
			colInfoMap = colInfoModel.getColInfoMap(updRes.getSubSys(), tableName);
		}
		
	}
	
	/**
	 * 更新主方法
	 * @return
	 * @throws BolueException
	 */
	public ActionResult update()throws BolueException{
		this.init();
		UpdRes updRes = this.updateInfo.getUpdRes();
		Integer updType = updRes.getUpdType();
		if(updType==1){//直接数据表操作
			return updateByTable();
		}else if(updType==3){//批量直接数据表操作
			return batchUpdateByTable();
		}
		return null;
	}
	
	/**直接数据表更新
	 * 
	 * @return
	 * @throws BolueException
	 */
	public ActionResult updateByTable()throws BolueException{
		UpdRes updRes = this.updateInfo.getUpdRes();
		String subSys = updRes.getSubSys();
		String dbResId = subSys;
		putUpdateData();
		putWhereData();
		
		boolean result = false;
		if(isUseBusi()){
			busiUpdate();
		}else{
			DbOpUtil dbOpUtil = new DbOpUtil(dbResId);
			Map<String, Object> updateMap = this.updateInfo.getUpdateDataMap();
			Map<String, Object> whereMap = this.updateInfo.getWhereDataMap();
			dbOpUtil.update(updRes.getTableName(), updateMap, whereMap);
		}
		
		ActionResult actionResult = new ActionResult();
		result = true;
		actionResult.setResult(result);
		return actionResult;
	}
	
	public void busiUpdate() throws BolueException{
		BusiBase busiBase = null;
		UpdRes updRes = this.updateInfo.getUpdRes();
		String busiClass = updRes.getBusiClass();
		Object localObject = ReflectHelper.classNewInstance(busiClass);
		if(localObject!=null&&(localObject instanceof BusiBase)){
			busiBase = (BusiBase)localObject;
		}
		String busiMethod = updRes.getBusiMethod();
		busiMethod = busiMethod + "(java.util.Map,java.util.Map)";
		Object[] paramArray = new Object[2];
		Map<String,Object> updateMap = this.updateInfo.getUpdateDataMap();
		Map<String, Object> whereMap = this.updateInfo.getWhereDataMap();
		paramArray[0] = updateMap;
		paramArray[1] = whereMap;
		ReflectHelper.invokeMethod(busiBase, busiMethod, paramArray);
	}
	
	/**
	 * 数据表批量更新
	 * @return
	 * @throws BolueException
	 */
	public ActionResult batchUpdateByTable()throws BolueException{
		initBatch();
		UpdRes updRes = this.updateInfo.getUpdRes();
		String[] updColArray = this.updateInfo.getUpdColArray();
		String[] whereColArray = this.updateInfo.getWhereColArray();
		if(updColArray==null||updColArray.length<1){
			return null;
		}
		String updateSql = DbCreateSqlUtil.createUpdateSql(updRes.getTableName(), updColArray, whereColArray);
		if(updateSql==null){
			return null;
		}
		Integer dataSize = 0;
		String _recordNum = request.getParameter("recordNum");
		if(_recordNum!=null){
			dataSize = Integer.parseInt(_recordNum);
		}
		
		List<Object[]> dataList = new ArrayList<Object[]>();
		Integer cellNum = updColArray.length+whereColArray.length;
		for(int i=0;i<dataSize;i++){
			Object[] record = new Object[cellNum];
			for(int j=0;j<cellNum;j++){
				for(String updCol : updColArray){
					String colValue = request.getParameter(updCol);
					if(colValue!=null){
						String[] colValueA = colValue.split(",");
						if(colValueA.length>i){
							record[j] = colValueA[i];
						}
					}
					j++;
				}
				for(String pkname : whereColArray){
					String colValue = request.getParameter(pkname);
					if(colValue!=null){
						String[] colValueA = colValue.split(",");
						if(colValueA.length>i){
							record[j] = colValueA[i];
						}
					}
					j++;
				}
			}
			dataList.add(record);
		}
		
		String subSys = updRes.getSubSys();
		String dbResId = subSys;
		DbOpUtil dbOpUtil = new DbOpUtil(dbResId);
		dbOpUtil.update(updateSql, dataList);
		
		ActionResult actionResult = new ActionResult();
		boolean result = true;
		actionResult.setResult(result);
		return actionResult;
	}
	
	/**
	 * 
	 * 初始批量更新表列
	 */
	private void initBatch(){
		UpdRes updRes = this.updateInfo.getUpdRes();
		//获得要更新的列
		String updCols = updRes.getUpdCols();
		String[] updColArray = new String[]{};
		if(updCols!=null){
			updColArray = updCols.split(",");
		}
		this.updateInfo.setUpdColArray(updColArray);
		List<ColInfo> keyColInfoList = this.updateInfo.getKeyColInfoList();
		//获得条件列
		String[] pknames = new String[keyColInfoList.size()];
		for(int i=0;i<keyColInfoList.size();i++){
			ColInfo colInfo = keyColInfoList.get(i);
			String pkName = colInfo.getColName();
			pknames[i] = pkName;
		}
		this.updateInfo.setWhereColArray(pknames);
	}
	
	/**
	 * 获得要插入的数据
	 */
	private void putUpdateData(){
		Map<String,Object> updateMap = new HashMap<String,Object>();
		UpdRes updRes = this.updateInfo.getUpdRes();
		String updCols = updRes.getUpdCols();
		if(updCols!=null){
			Map<String, ColInfo> colInfoMap = this.updateInfo.getColInfoMap();
			String[] updColArray = updCols.split(",");
			if(updColArray!=null&&updColArray.length>0){
				for(int i=0;i<updColArray.length;i++){
					String updCol = updColArray[i];
					String updColValue = RequestUtil.getColParamValue(request, updCol);
					ColInfo colInfo = colInfoMap.get(updCol);
					Object parseValue =  RequestUtil.parseValue(colInfo.getJType(),updColValue);
					updateMap.put(updCol, parseValue);
				}
			}
		}
		this.updateInfo.setUpdateDataMap(updateMap);
	}
	
	/**
	 * 获得条件数据
	 * @throws BolueException
	 */
	private void putWhereData()throws BolueException{
		List<ColInfo> colInfoList = this.updateInfo.getColInfoList();
		if(colInfoList==null){
			return;
		}
		Map<String,Object> whereMap = new HashMap<String,Object>();
		for(ColInfo colInfo : colInfoList){
			String keyName = colInfo.getColName();
			String keyValue = request.getParameter("orig_"+keyName);
			if(colInfo.getIsPkey()==1&&(keyValue==null||"".equals(keyValue))){
				throw new BolueException(1, "在request中，必须提供参数"+"orig_"+keyName+"!");
			}
			if(colInfo.getIsPkey()==1&&(keyValue!=null&&!"".equals(keyValue))){
				Object parseValue =  RequestUtil.parseValue(colInfo.getJType(),keyValue);
				whereMap.put(keyName, parseValue);
			}
		}
		this.updateInfo.setWhereDataMap(whereMap);
	}
	
	/**
	 * 是否使用业务类
	 * @return
	 */
	private boolean isUseBusi(){
		UpdRes updRes = this.updateInfo.getUpdRes();
		String busiClass = updRes.getBusiClass();
		String busiMethod = updRes.getBusiMethod();
		if(busiClass!=null&&!"".equals(busiClass)&&busiMethod!=null&&!"".equals(busiMethod)){
			return true;
		}
		return false;
	}
}
